package com.longnuoqi.mydoctor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.longnuoqi.mydoctor.entity.Favorite;
import com.yunhe.mydoctor.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by Lenovo on 2015/12/25.
 */
public class UserFavoriteListAdapter extends BaseAdapter {
    private List<Favorite> list;
    private Context context;
    private LayoutInflater inflater;

    public UserFavoriteListAdapter(Context context, List<Favorite> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (list.size() != 0) {
            return list.size();
        } else return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.user_favorite_listview_item, null);
            holder.type = (TextView) convertView.findViewById(R.id.user_favorite_lv_type);
            holder.title = (TextView) convertView.findViewById(R.id.user_favorite_lv_txt);
            holder.time = (TextView) convertView.findViewById(R.id.user_favorite_lv_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        switch (list.get(position).getType()) {
            case "ask":
                holder.type.setText("问答");
                break;
            case "info":
                holder.type.setText("资讯");
                break;
            case "disease":
                holder.type.setText("疾病");
                break;
            case "hospital":
                holder.type.setText("医院");
                break;
            default:
                break;
        }
        holder.type.setTag(list.get(position).getType());
        holder.title.setText(list.get(position).getTitle());
        holder.title.setTag(list.get(position).getId());
        holder.time.setText(dateFormat(list.get(position).getTime()));
        return convertView;
    }

    private String dateFormat(String time) {
        long current = System.currentTimeMillis();
        long past = Long.parseLong(time);
        int second = (int) ((current - past) / 1000);
        int minute = second / 60;
        int hour = minute / 60;
        int day = hour / 24;
        if (second < 60) {
            return "收藏于" + second + "秒前";
        } else if (minute < 60) {
            return "收藏于" + minute + "分钟前";
        } else if (hour < 24) {
            return "收藏于" + hour + "小时前";
        } else if (day < 3) {
            return "收藏于" + day + "天前";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm");
            return "收藏于" + sdf.format(new Date(past));
        }
    }

    private class ViewHolder {
        public TextView type;
        public TextView title;
        public TextView time;
    }
}
